//package com.mrzhou.rabbit.consumer;
//
//import com.mrzhou.rabbit.constant.RabbitConstant;
//import com.mrzhou.rabbit.entity.EsMqMessage;
//import com.mrzhou.rabbit.entity.User;
//import com.mrzhou.rabbit.repository.UserRepository;
//import com.rabbitmq.client.Channel;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.rabbit.annotation.*;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.client.ResourceAccessException;
//
//import java.io.IOException;
//
///**
// * @author qiang.zhou
// */
//@Component
//@Slf4j
//public class ElasticsearchConsumer {
//
//    @Autowired
//    private UserRepository userRepository;
//
//    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = RabbitConstant.ELASTICSEARCH_QUEUE, durable = "true"),
//            exchange = @Exchange(value = RabbitConstant.ELASTICSEARCH_EXCHANGE, type = "topic"),
//            key = RabbitConstant.ELASTICSEARCH_QUEUE))
//    public void receive(EsMqMessage<User> message, Message msg, Channel channel) throws IOException {
//        long deliveryTag = msg.getMessageProperties().getDeliveryTag();
//        try{
//            switch (message.getAction()) {
//                case EsMqMessage.CREATE_OR_UPDATE:
//                    userRepository.save(message.getData());
//                    break;
//                case EsMqMessage.REMOVE:
//                    userRepository.deleteById(message.getId());
//                    break;
//                default:
//                    log.warn("未知的消息处理类型，异常的消息id: {}", message.getId());
//                    break;
//            }
//
//            channel.basicAck(deliveryTag, false);
//        } catch (IndexOutOfBoundsException e) {
//            //  抛弃此消息, 消息进入死信队列, 前提是队列设置了对应的死信队列
//            channel.basicNack(deliveryTag, false, false);
//        } catch (ResourceAccessException e) {
//            // 消息重新入列
//            channel.basicNack(deliveryTag, false, true);
//        }
//
//    }
//
//}
